package com.huangliusong.threadcurrency.example.atomic;

import com.huangliusong.threadcurrency.annotation.ThreadSafe;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author huangliusong
 * @since 2018/11/18
 * {@link ExecutorService}
 * {@link Semaphore}
 * {@link CountDownLatch}
 */
@Slf4j
@ThreadSafe
public class AtomicExample5 {
    private static AtomicIntegerFieldUpdater<AtomicExample5> updater=AtomicIntegerFieldUpdater
            .newUpdater(AtomicExample5.class,"count");

    @Getter
    private volatile int  count=100;

    private static AtomicExample5 atomicExample5=new AtomicExample5();


    public static void main(String[] args) {

        if (updater.compareAndSet(atomicExample5,100,120)){
            log.info("success1  updater:{}",atomicExample5.getCount());
        }

        if (updater.compareAndSet(atomicExample5,100,120)){
            log.info("success2  updater:{}",atomicExample5.getCount());
        }else {
            log.info("update failed,{}",atomicExample5.getCount());
        }

    }
}
